<template>
  <div class="register-form">
    <!-- 动态步骤条 -->
    <div class="step-line">
      <el-steps :active="currentStep" align-center finish-status="success" style="max-width: 600px">
        <el-step title="手机验证" description="输入手机号并验证" />
        <el-step title="设置密码" description="设置您的账户密码" />
        <el-step title="注册完成" description="完成注册流程" />
      </el-steps>
    </div>

    <!-- 表单内容 -->
    <div class="form-content">
      <!-- 第一步：手机验证 -->
      <div v-if="currentStep === 1" class="phone-code">
        <el-input v-model="phoneNumber" style="width: 300px; margin-bottom: 20px;" placeholder="请输入手机号"
          :disabled="verifySuccess">
          <template #prepend>
            <el-select v-model="areaCode" placeholder="中国 +86" style="width: 115px" :disabled="verifySuccess">
              <el-option label="中国 +86" value="86" />
              <el-option label="日本 +81" value="81" />
              <el-option label="美国 +1" value="1" />
              <el-option label="英国 +44" value="44" />
            </el-select>
          </template>
        </el-input>

        <!-- 滑块验证 -->
        <div class="slider-verify" v-if="!verifySuccess && phoneNumber">
          <div class="slider-container">
            <div class="slider-track" ref="track">
              <div class="slider-thumb" :style="{ left: thumbLeft + 'px' }" @mousedown="handleStart"></div>
              <span class="slider-text">
                请向右滑动滑块完成验证
              </span>
            </div>
          </div>
        </div>

        <!-- 验证码输入 -->
        <div v-if="verifySuccess" class="code-input">
          <el-input v-model="smsCode" placeholder="请输入验证码" style="width: 300px; margin-right: 10px;"
            maxlength="6"></el-input>
          <el-button type="primary" :disabled="!canResend" @click="sendSmsCode">
            {{ countdown > 0 ? `${countdown}秒后重发` : '获取验证码' }}
          </el-button>
        </div>

        <el-button type="primary" class="next-btn" :disabled="!canGoNext" @click="nextStep">
          同意协议并继续
        </el-button>
        <div class="agreement">
          <el-checkbox v-model="isAgreed">我已阅读并同意</el-checkbox>
          <a href="https://www.jd.com" target="_blank">《京东用户服务协议和隐私政策》</a>
        </div>
      </div>

      <!-- 第二步：设置密码 -->
      <div v-if="currentStep === 2" class="password-form">
        <el-form label-width="80px">
          <el-form-item label="设置密码">
            <el-input v-model="password" type="password" placeholder="请输入密码" show-password
              style="width: 300px;"></el-input>
          </el-form-item>
          <el-form-item label="确认密码">
            <el-input v-model="confirmPassword" type="password" placeholder="请再次输入密码" show-password
              style="width: 300px;"></el-input>
          </el-form-item>
        </el-form>

        <div class="action-buttons">
          <el-button @click="prevStep">上一步</el-button>
          <el-button type="primary" @click="nextStep">下一步</el-button>
        </div>
      </div>

      <!-- 第三步：注册完成 -->
      <div v-if="currentStep === 3" class="complete-step">
        <el-result icon="success" title="注册成功" sub-title="恭喜您已完成注册流程">
          <template #extra>
            <el-button type="primary" @click="goToLogin">前往登录</el-button>
          </template>
        </el-result>
      </div>

    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import router from '@/router'
// 步骤控制
const currentStep = ref(1)

// 第一步数据
const phoneNumber = ref('')
const areaCode = ref('86')
const verifySuccess = ref(false)
const smsCode = ref('')
const countdown = ref(0)
const canResend = computed(() => countdown.value === 0)

// 第二步数据
const password = ref('')
const confirmPassword = ref('')

// 滑块验证相关
const track = ref(null)
const thumbLeft = ref(0)
let isDragging = false
let startX = 0
let thumbX = 0

// 是否可以进入下一步
const canGoNext = computed(() => {
  if (currentStep.value === 1) {
    return verifySuccess.value && smsCode.value.length === 6
  } else if (currentStep.value === 2) {
    return password.value && password.value === confirmPassword.value
  }
  return true
})

// 发送验证码
const sendSmsCode = () => {
  if (!phoneNumber.value) {
    ElMessage.warning('请输入手机号')
    return
  }

  if (!/^1[3-9]\d{9}$/.test(phoneNumber.value)) {
    ElMessage.warning('请输入正确的手机号')
    return
  }

  countdown.value = 60
  const timer = setInterval(() => {
    countdown.value--
    if (countdown.value <= 0) {
      clearInterval(timer)
    }
  }, 1000)

  ElMessage.success('验证码已发送')
}

// 滑块验证开始
const handleStart = (e) => {
  isDragging = true
  startX = e.clientX
  thumbX = thumbLeft.value
  e.preventDefault()
}

// 滑块验证移动
const handleMove = (e) => {
  if (!isDragging) return

  const deltaX = e.clientX - startX
  let newLeft = thumbX + deltaX
  const maxLeft = track.value?.offsetWidth - 40 || 260

  newLeft = Math.max(0, Math.min(newLeft, maxLeft))
  thumbLeft.value = newLeft

  if (newLeft >= maxLeft - 5) {
    verifySuccess.value = true
    track.value.style.backgroundColor = '#67c23a'
    isDragging = false
    ElMessage.success('验证通过')
  }
}

// 滑块验证结束
const handleEnd = () => {
  if (!isDragging) return
  isDragging = false

  if (!verifySuccess.value) {
    thumbLeft.value = 0
  }
}

// 下一步
const nextStep = () => {
  if (currentStep.value < 3) {
    currentStep.value++
  }
}

// 上一步
const prevStep = () => {
  if (currentStep.value > 1) {
    currentStep.value--
  }
}

// 完成注册
const goToLogin = () => {
  ElMessage.success('即将跳转到登录页面')
  router.push('./login')
}

onMounted(() => {
  document.addEventListener('mousemove', handleMove)
  document.addEventListener('mouseup', handleEnd)
})
</script>

<style scoped lang="scss">
.register-form {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;
  width: 100%;
  max-width: 800px;
  margin: 0 auto;

  .step-line {
    width: 100%;
    margin: 100px 0 40px;
    display: flex;
    justify-content: center;
    align-items: center;

    :deep(.el-steps) {
      justify-content: center;
      max-width: 600px;

      .el-step {
        flex: 0 1 auto;
        min-width: 120px;

        &:last-child {
          .el-step__line {
            display: none;
          }
        }
      }
    }
  }

  .form-content {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;

    .phone-code {
      display: flex;
      flex-direction: column;
      align-items: center;
      width: 100%;

      .el-input {
        width: 300px;
        margin-bottom: 20px;
      }

      .slider-verify {
        margin: 20px 0;
        width: 100%;
        display: flex;
        justify-content: center;

        .slider-container {
          width: 300px;
          margin: 10px 0;

          .slider-track {
            width: 100%;
            height: 40px;
            background-color: #f1f1f1;
            border-radius: 20px;
            position: relative;
            overflow: hidden;

            .slider-thumb {
              width: 40px;
              height: 40px;
              background-color: #409eff;
              border-radius: 50%;
              position: absolute;
              left: 0;
              top: 0;
              cursor: grab;
              z-index: 2;
              transition: all 0.3s;

              &:active {
                cursor: grabbing;
              }
            }

            .slider-text {
              position: absolute;
              width: 100%;
              text-align: center;
              line-height: 40px;
              color: #666;
              user-select: none;
              z-index: 1;
            }
          }
        }
      }

      .code-input {
        display: flex;
        justify-content: center;
        margin-bottom: 20px;
        width: 100%;

        .el-input {
          width: 300px;
          margin-right: 10px;
        }
      }

      .next-btn {
        margin-top: 20px;
        width: 300px;
        font-size: 12px;
        background-color: #e1251b;
        color: white;
        border: none;
        transition: background-color 0.3s;

        &:hover {
          background-color: darken(#e1251b, 10%);
        }

        &:disabled {
          background-color: #f5f5f5;
          color: #c0c4cc;
          cursor: not-allowed;
        }
      }

      .agreement {
        margin: 16px 0;
        display: flex;
        justify-content: center;
        width: 100%;

        :deep(.el-checkbox) {
          .el-checkbox__label {
            display: flex;
            align-items: center;
            font-size: 12px;
            color: #666;

            a {
              color: #e1251b;
              text-decoration: none;
              margin-left: 4px;
              transition: color 0.2s;

              &:hover {
                color: darken(#e1251b, 10%);
                text-decoration: underline;
              }
            }
          }
        }
      }
    }

    .password-form {
      width: 100%;
      display: flex;
      flex-direction: column;
      align-items: center;

      .el-form {
        width: 100%;
        display: flex;
        flex-direction: column;
        align-items: center;

        .el-form-item {
          width: 300px;
          margin-bottom: 20px;

          .el-input {
            width: 100%;
          }
        }
      }

      .action-buttons {
        margin-top: 20px;
        display: flex;
        gap: 20px;
      }
    }

    .complete-step {
      width: 100%;
      padding: 40px 0;
    }
  }
}
</style>
